package com.ss.android.image;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.io.NetHackDbg;
import com.ss.android.article.browser.R;
import com.ss.android.common.app.permission.PermissionsManager;
import com.ss.android.common.util.ToolUtils;

import java.io.File;

public class BaseImageManager {
    public static String a = "/funnygallery";
    public static volatile boolean h = false;

    final String e = Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.ss.spipe/cache/avatar";
    public Context mContext;
    String mExternalImageDir;
    String mExternalTmpImageDir;
    String mInternalImageDir;
    String mExternalFunnyImageDir;
    private String mPackageName;
    private String mExternalCacheDir;
    private String mInternalCacheDir;

    public BaseImageManager(Context context) {
        this(context, (byte) 0);
    }

    private BaseImageManager(Context context, byte arg5) {
        this.mContext = context.getApplicationContext();
        this.mPackageName = context.getPackageName();
        this.mExternalCacheDir = Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + this.mPackageName + "/cache/";
        try {
            this.mInternalCacheDir = ToolUtils.getInternalCacheDir(context);
        } catch (Exception e) {
        }
        this.mInternalImageDir = !android.support.design.a.isTextEmpty(this.mInternalCacheDir) ? this.mInternalCacheDir + "/hashedimages/" : null;
        this.mExternalImageDir = this.mExternalCacheDir + "hashedimages/";
        this.mExternalTmpImageDir = this.mExternalCacheDir + "tmpimages/";
        this.mExternalFunnyImageDir = Environment.getExternalStorageDirectory().getPath() + a;
        if (isExternalStorageMounted()) {
            File externalCacheDir = new File(this.mExternalCacheDir);
            if (!externalCacheDir.exists()) {
                externalCacheDir.mkdirs();
            }

            File externalImageDir = new File(this.mExternalImageDir);
            if (!externalImageDir.exists()) {
                externalImageDir.mkdirs();
            }

            File externalTmpImageDir = new File(this.mExternalTmpImageDir);
            if (!externalTmpImageDir.exists()) {
                externalTmpImageDir.mkdirs();
            }
        }

        try {
            if (android.support.design.a.isTextEmpty(this.mInternalImageDir)) {
                return;
            }

            File internalImageDir = new File(this.mInternalImageDir);
            if (!internalImageDir.exists()) {
                internalImageDir.mkdirs();
            }
        } catch (Exception e) {
        }
    }

    public static Bitmap createRoundRectBitmap(Bitmap bitmap, int round) {
        if (bitmap == null) {
            return null;
        } else {
            Bitmap roundBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(roundBitmap);
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            RectF rectF = new RectF(rect);
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(-12434878);
            canvas.drawRoundRect(rectF, round, round, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return roundBitmap;
        }
    }

    static String getSuffix(String arg5, String arg6) {
        String v0 = ".jpg";
        if (arg6 == null) {
            if (arg5 != null) {
//                goto label_4;
            } else {
                return v0;
            }
        }

        try {
//            label_4:
            NetHackDbg.ImageType imageType = NetHackDbg.b(arg5);
            switch (imageType) {
                case JPG: {
                    v0 = ".jpg";
                    break;
                }
                case PNG: {
                    v0 = ".png";
                    break;
                }
                case GIF: {
                    v0 = ".gif";
                    break;
                }
            }

            if (!NetHackDbg.ImageType.UNKNOWN.equals(imageType)) {
                return v0;
            }

            String v1_2 = Uri.parse(arg6).getPath();
            if (v1_2 == null) {
                return v0;
            }

            int v2 = v1_2.lastIndexOf(46);
            if (v2 <= 0) {
                return v0;
            }

            if (v2 + 1 >= v1_2.length()) {
                return v0;
            }

            v1_2 = v1_2.substring(v2);
            if (!v1_2.equalsIgnoreCase(".jpg") && !v1_2.equalsIgnoreCase(".jpeg") && !v1_2.equalsIgnoreCase(".png") && !v1_2.equalsIgnoreCase(".gif") && !v1_2.equalsIgnoreCase(".bmp")) {
                return v0;
            }

            v0 = v1_2;
        } catch (Exception v1) {
            Logger.w("BaseImageManager", "getSuffix exception " + v1);
        }

        return v0;
    }

    static void a(int arg5, File arg6) {
        if ((arg6.exists()) && (arg6.isDirectory())) {
            File[] v1 = arg6.listFiles();
            if (v1 != null && v1.length != 0) {
                int v2 = v1.length;
                int v0;
                for (v0 = 0; v0 < v2; ++v0) {
                    File v3 = v1[v0];
                    if (v3.isDirectory()) {
                        b(arg5, v3);
                    }
                }
            }
        }
    }

    public static boolean a() {
        String externalStorageState = Environment.getExternalStorageState();
        return ("mounted".equals(externalStorageState))
                || ("mounted_ro".equals(externalStorageState));
    }

    public static boolean isExternalStorageMounted() {
        try {
            return "mounted".equals(Environment.getExternalStorageState());
        } catch (Exception e) {
            if (Logger.debug()) {
                e.printStackTrace();
            }
            return false;
        }
    }

    static void b(int arg10, File file) {
        if ((file.exists()) && (file.isDirectory())) {
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }

            if (files.length == 0) {
                return;
            }

            long v2 = System.currentTimeMillis();
            long v4 = (((long) (arg10 * 24 * 3600))) * 1000;
            int v6 = files.length;
            int v0;
            for (v0 = 0; v0 < v6; ++v0) {
                File v7 = files[v0];
                if (v7.isFile()) {
                    v7.getName();
                    try {
                        if (v2 - v7.lastModified() <= v4) {
                            continue;
                        }
                        v7.delete();
                    } catch (Exception v7_1) {
                    }
                }
            }
        }
    }

    public static Bitmap createRoundBitmap(Bitmap bitmap, int roundCornerPixel) {
        int size = roundCornerPixel * 2;
        Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(-1);
        canvas.drawCircle(roundCornerPixel, roundCornerPixel, roundCornerPixel, paint);
        Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        Rect dst = new Rect(0, 0, size, size);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint);
        return output;
    }

    public static String getCacheFileName(String key) {
        return key + ".dat";
    }

    private static String getCacheFileParentDir(String key) {
        if (key == null || key.length() < 2) {
            return "__";
        } else {
            String v0 = key.substring(0, 3);
            char firstChar = key.charAt(0);
            char secondChar = key.charAt(1);
            if (Character.isLetterOrDigit(firstChar)
                    && Character.isLetterOrDigit(secondChar)) {
                return v0;
            } else {
                return "__";
            }
        }
    }

    public final Bitmap a(String arg3, int arg4, int arg5) {
        String v0 = this.getExternalCacheFile(arg3);
        if (!new File(v0).isFile()) {
            v0 = this.getInternalCacheFile(arg3);
        }

        return android.support.design.a.decodeFile(v0, arg4, arg5, null);
    }

    public final void saveImage(Context context, String arg16, String arg17) {
        String v7;
        File v8;
        String v5;
        int v13 = R.string.toast_download_failed;
        boolean v2 = false;
        int v12 = R.drawable.close_popup_textpage;
        String v6 = null;
        try {
            if (!android.support.design.a.isTextEmpty(arg16)) {
                v5 = this.getExternalCacheFile(arg16);
                v8 = new File(v5);
                v2 = v8.isFile();
                if (!v2) {
                    v5 = this.getInternalCacheFile(arg16);
                    v8 = new File(v5);
                    v2 = v8.isFile();
                }
            } else {
                v8 = null;
                v5 = v6;
            }

            if (v2) {
                v6 = arg17;
                v7 = arg16;
            } else if (!android.support.design.a.isTextEmpty((String) null)) {
                v5 = this.getExternalCacheFile(null);
                v8 = new File(v5);
                v2 = v8.isFile();
                if (!v2) {
                    v5 = this.getInternalCacheFile(null);
                    v8 = new File(v5);
                    v2 = v8.isFile();
                    v7 = v6;
                } else {
                    v7 = v6;
                }
            } else {
                v6 = arg17;
                v7 = arg16;
            }

            if (!v2) {
                UIUtils.showImageToast(context, R.drawable.close_popup_textpage, R.string.toast_download_not_cached);
                return;
            }

            if (Build.VERSION.SDK_INT >= 23) {
                if (!(context instanceof Activity)) {
                    return;
                }
                PermissionsManager.getInstance().requestPermission((Activity) context,
                        new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
                        new f(this, context, v5, v6, v7, v8));
                return;
            }

            String v3 = v7 + getSuffix(v5, v6);
            String v4 = this.mExternalFunnyImageDir;
            String v2_2 = v4.endsWith("/") ? v4 + v3 : v4 + "/" + v3;
            File v6_1 = new File(v2_2);
            if ((v6_1.isFile()) && v8.length() == v6_1.length()) {
                UIUtils.showImageToast(context, R.drawable.doneicon_popup_textpage, R.string.toast_download_successful);
                return;
            }

            if (!NetHackDbg.a(NetHackDbg.a(v5), v4, v3)) {
                UIUtils.showImageToast(context, R.drawable.close_popup_textpage, R.string.toast_download_failed);
                return;
            }
            ToolUtils.sendMediaScannerScanFileBroadcast(context, v2_2);
            UIUtils.showImageToast(context, R.drawable.doneicon_popup_textpage, R.string.toast_download_successful);
        } catch (Exception e) {
            UIUtils.showImageToast(context, v12, v13);
        }
    }

    public final boolean existsInExternalCache(String key) {
        return new File(this.getExternalCacheFile(key)).exists();
    }

    public final String getExternalImageSaveDir(String key) {
        return this.mExternalImageDir + getCacheFileParentDir(key);
    }

    public final void b(Context context, String arg12, String arg13) {
        int v9 = R.string.toast_download_successful;
        int v8 = R.drawable.close_popup_textpage;
        File file = FrescoUtils.tryGetCacheFile(Uri.parse(arg13));
        if (file == null || !file.exists()) {
            UIUtils.showImageToast(context, v8, R.string.toast_download_not_cached);
        } else if (Build.VERSION.SDK_INT < 23) {
            String v0 = arg12 + getSuffix(file.getAbsolutePath(), arg13);
            String v1 = this.mExternalFunnyImageDir;
            v0 = v1.endsWith("/") ? v1 + v0 : v1 + "/" + v0;
            File v2 = new File(v0);
            if ((v2.isFile()) && file.length() == v2.length()) {
                UIUtils.showImageToast(context, v8, v9);
                return;
            }

            if (!NetHackDbg.a(file.getAbsolutePath(), v1, v2.getName())) {
                UIUtils.showImageToast(context, v8, R.string.toast_download_failed);
                return;
            }

            ToolUtils.sendMediaScannerScanFileBroadcast(context, v0);
            UIUtils.showImageToast(context, R.drawable.doneicon_popup_textpage, v9);
        } else if ((context instanceof Activity)) {
            PermissionsManager.getInstance().requestPermission((Activity) context, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, new e(this, context, file, arg13, arg12));
        }
    }

    public final void c() {
        try {
            if (!android.support.design.a.isTextEmpty(this.mInternalImageDir)) {
                ToolUtils.d(this.mInternalImageDir);
            }

            if (!isExternalStorageMounted()) {
                return;
            }

            ToolUtils.d(this.mExternalImageDir);
        } catch (Exception v0) {
        }
    }

    public final long d() {
        long v0 = 0;
        try {
            v0 += ToolUtils.a(new File(this.mExternalCacheDir), false);
        } catch (Throwable v2) {
        }

        return v0;
    }

    public final String getExternalCacheFile(String key) {
        return this.mExternalImageDir
                + getCacheFileParentDir(key)
                + "/"
                + key
                + ".dat";
    }

    public final String getInternalImageSaveDir(String key) {
        return !android.support.design.a.isTextEmpty(this.mInternalImageDir) ? this.mInternalImageDir + getCacheFileParentDir(key) : null;
    }

    public final String getInternalCacheFile(String key) {
        if (android.support.design.a.isTextEmpty(this.mInternalImageDir)) {
            return null;
        } else {
            return this.mInternalImageDir + getCacheFileParentDir(key) + "/" + key + ".dat";
        }
    }
}

